package com.yyf.hap.sale.service.impl;

import com.hand.hap.system.service.impl.BaseServiceImpl;
import com.yyf.hap.sale.demo.ExcelUtil;
import com.yyf.hap.sale.entity.ExcelBean;
import com.yyf.hap.sale.entity.Salarymanage;
import com.yyf.hap.sale.mapper.ManageMapper;
import com.yyf.hap.sale.utils.DateUtil;
import com.yyf.hap.sale.utils.UUIDUtils;
import org.apache.http.ParseException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yyf.hap.sale.dto.Manage;
import com.yyf.hap.sale.service.IManageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ManageServiceImpl extends BaseServiceImpl<Manage> implements IManageService{


	@Autowired
	private ManageMapper manageMapper;


	/**
	 * Excel表导入
	 * @param in
	 * @param file
	 * @param month
	 * @param adminId
	 */
	@Override
	public void importExcelInfo(InputStream in, MultipartFile file, String month, Long adminId) throws Exception {
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,file.getOriginalFilename());
		List<Manage> salaryList = new ArrayList<Manage>();
		//遍历listob数据，把数据放到List中
		for (int i = 0; i < listob.size(); i++) {
			List<Object> ob = listob.get(i);
			Manage manage = new Manage();
			//设置编号
			manage.setSerial(UUIDUtils.UUIDGenerator());
			//通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
			manage.setAdminId(adminId);
			manage.setCompany(String.valueOf(ob.get(1)));
			manage.setNumber(String.valueOf(ob.get(2)));
			manage.setName(String.valueOf(ob.get(3)));
			manage.setSex(String.valueOf(ob.get(4)));
			manage.setCardName(String.valueOf(ob.get(5)));
			manage.setBankCard(String.valueOf(ob.get(6)));
			manage.setBank(String.valueOf(ob.get(7)));

			//object类型转Double类型
			manage.setMoney(Double.parseDouble(ob.get(8).toString()));
			manage.setRemark(String.valueOf(ob.get(9)));
			manage.setSalaryDate(month);
			salaryList.add(manage);
		}
		//批量插入
		manageMapper.insertInfoBatch(salaryList);
	}

	/**
	 *
	 * @param
	 * @return
	 * @throws InvocationTargetException
	 * @throws ClassNotFoundException
	 * @throws IntrospectionException
	 * @throws ParseException
	 * @throws IllegalAccessException
	 */
	@Override
	public XSSFWorkbook exportExcelInfo()
			throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {
		//根据条件查询数据，把数据装载到一个list中
		List<Manage> list = manageMapper.selectApartInfo();
		for(int i=0;i<list.size();i++){
			//查询财务名字
			long adminId = list.get(i).getAdminId();
			String adminName = manageMapper.selectAdminNameById(adminId);
			list.get(i).setName(adminName);
			list.get(i).setAdminId((long) (i+1));
		}
		List<ExcelBean> excel=new ArrayList<>();
		Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
		XSSFWorkbook xssfWorkbook = null;
		//设置标题栏
		excel.add(new ExcelBean("序号","adminId",0));
		excel.add(new ExcelBean("厂名","company",0));
		excel.add(new ExcelBean("工号","number",0));
		excel.add(new ExcelBean("姓名","name",0));
		excel.add(new ExcelBean("性别","sex",0));
		excel.add(new ExcelBean("开户名","cardName",0));
		excel.add(new ExcelBean("银行卡号","bankCard",0));
		excel.add(new ExcelBean("开户行","bank",0));
		excel.add(new ExcelBean("金额","money",0));
		excel.add(new ExcelBean("备注","remark",0));
		map.put(0, excel);
		String sheetName = "订单信息";
		//调用ExcelUtil的方法
		xssfWorkbook = ExcelUtil.createExcelFile(Manage.class, list, map, sheetName);
		return xssfWorkbook;
	}
}